package com.example.mptnet;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentTransaction;

import com.example.mptnet.Interfaces.API;
import com.example.mptnet.Models.Mailing;
import com.example.mptnet.Models.RP;
import com.example.mptnet.Models.User;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.Calendar;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UserWindow extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    private DrawerLayout drawerLayout;
    SharedPreferences settings;
    API api;
    Retrofit retrofit;
    ArrayList<RP> rp = new ArrayList<RP>();
    String UID = "";
    User user = new User();
    NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_window);
        Toolbar toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.layout);
        notificationManager = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NavigationView navigationView = findViewById(R.id.header_view);
        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open,
                R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MainFragment()).commit();
            navigationView.setCheckedItem(R.id.header_view);
        }
        settings = getSharedPreferences("SETTINGS",MODE_PRIVATE);
        retrofit = Builder.request();
        api = retrofit.create(API.class);
        UID = settings.getString("UID", "");
        Call<User> calluser = api.GetUser(UID);
        Call<Mailing> callmailing = api.GetMailing(UID);
        calluser.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()){
                    if(!response.body().getUsers_group().equals("default")){
                        callmailing.enqueue(new Callback<Mailing>() {
                            @Override
                            public void onResponse(Call<Mailing> call, Response<Mailing> response) {
                                if(response.isSuccessful()){

                                    Intent notificationIntent = new Intent(getApplicationContext(), Receiver.class);
                                    PendingIntent pendingIntent = PendingIntent.getBroadcast(UserWindow.this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
                                    Calendar calendar = Calendar.getInstance();
                                    calendar.setTimeInMillis(System.currentTimeMillis());
                                    /*calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(response.body().getTime().toString().substring(0,2))); // set hour
                                    calendar.set(Calendar.MINUTE, Integer.parseInt(response.body().getTime().toString().substring(3,5)));// set minute
                                    calendar.set(Calendar.SECOND, Integer.parseInt(response.body().getTime().toString().substring(6,8)));// set second*/
                                    calendar.set(Calendar.HOUR_OF_DAY,10);
                                    calendar.set(Calendar.MINUTE,40);
                                    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
                                }
                            }

                            @Override
                            public void onFailure(Call<Mailing> call, Throwable t) {

                            }
                        });
                    }
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        if (id == R.id.main)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MainFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
        if (id == R.id.rp)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new rp_fragment()).commit();
        if(id == R.id.settings)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Settings()).commit();
        if(id == R.id.zakaz)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NewOrder()).commit();
        if(id == R.id.profile)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Profile()).commit();
        if(id == R.id.help)
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Help()).commit();
        if(id == R.id.worker) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Worker()).commit();
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return onOptionsItemSelected(menuItem);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
}